package com.example.hello.client.ListBox;

import com.vaadin.shared.AbstractComponentState;

import java.util.LinkedList;
import java.util.List;

public class ListBoxState extends AbstractComponentState {

    private List<String> items = new LinkedList<String>();
    private List<Boolean> selected = new LinkedList<Boolean>();
    private List<String> values = new LinkedList<String>();

    private boolean multipleSelect = false;
    private int visibleItems = 1;

    public List<String> getItems() {
        return items;
    }

    public void setItems(List<String> items) {
        this.items = items;
        for (int i = 0; i < items.size(); i++) {
            selected.add(false);
            values.add(null);
        }
    }

    public void removeAllItems() {
        items.clear();
        selected.clear();
        values.clear();
    }

    public void addItem(String item) {
        addItem(items.size(), item);
    }

    public void addItem(int index, String item) {
        items.add(index, item);
        selected.add(index, false);
        values.add(index, null);
    }

    public boolean removeItem(String item) {
        return removeItem(items.indexOf(item) - 1) != null;
    }

    public String removeItem(int index) {
        selected.remove(index);
        values.remove(index);
        return items.remove(index);
    }

    public List<Boolean> getSelected() {
        return selected;
    }

    public void setSelected(List<Boolean> selected) {
        this.selected = selected;
    }

    public boolean getSelected(int index) {
        return selected.get(index);
    }

    public void setItemSelected(int index, boolean selected) {
        this.selected.set(index, selected);
    }

    public boolean isItemSelected(int index) {
        return selected.get(index);
    }

    public List<String> getValues() {
        return values;
    }

    public void setValues(List<String> values) {
        this.values = values;
    }

    public String getValue(int index) {
        return values.get(index);
    }

    public void setValue(int index, String value) {
        values.set(index, value);
    }

    public void setItemText(int index, String text) {
        items.set(index, text);
    }

    public boolean isMultipleSelect() {
        return multipleSelect;
    }

    public void setMultipleSelect(boolean multipleSelect) {
        this.multipleSelect = multipleSelect;
    }

    public int getSelectedIndex() {
        for (int i = 0; i < selected.size(); i++)
            if (selected.get(i)) return i;
        return -1;
    }

    public void setSelectedIndex(int index) {
        for (int i = 0; i < selected.size(); i++)
            selected.set(i, false);
        selected.set(index, true);
    }

    public int getVisibleItems() {
        return visibleItems;
    }

    public void setVisibleItems(int visibleItems) {
        this.visibleItems = visibleItems;
    }

}
